<template>
  <div class="employees-detail-container">
    <div class="app-container">
      <el-card>
        <!-- 放置表单 -->
        <el-form
          label-width="120px"
          style="margin-left: 120px; margin-top: 30px"
          ref="userForm"
          :model="form"
          :rules="rules"
        >
          <el-form-item label="姓名:" prop="username">
            <el-input style="width: 300px" v-model="form.username" />
          </el-form-item>
          <el-form-item label="手机">
            <el-input style="width: 300px" disabled v-model="form.mobile" />
          </el-form-item>

          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker
              type="date"
              class="input"
              v-model="form.timeOfEntry"
            />
          </el-form-item>
          <el-form-item label="员工头像">
            <UploadImg ref="Uimg" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit">更新</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getEmpInfoAPI, editEmpInfoAPI } from '@/api/employees'
export default {
  name: 'EmployeesDetail',
  data () {
    return {
      form: {},
      rules: {
        username: [
          {
            required: true,
            message: '姓名不能为空',
            trigger: ['blur', 'change']
          }
        ],
        timeOfEntry: [
          {
            required: true,
            message: '请选择入职时间',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    /**
     * 获取id变化
     */
    getId () {
      return this.$route.params.id
    }
  },
  methods: {
    submit () {
      this.form.staffPhoto = this.$refs.Uimg.fileList[0].url

      this.$refs.userForm
        .validate()
        .then(async () => {
          console.log(1)
          await editEmpInfoAPI(this.form)
          this.$message.success('修改成功！')
        })
        .catch(() => {})
    },
    /**
     * 获取员工基本信息
     */
    async getEmpInfo () {
      const {
        data: { data }
      } = await getEmpInfoAPI(this.getId)
      this.form = data
      this.$refs.Uimg.fileList = [{ url: data.staffPhoto }]
    }
  },
  created () {
    this.getEmpInfo()
  }
}
</script>
